package com.longrise.android.byjk.plugins.hra.hratabfirst;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.byjk.R;
import com.longrise.android.byjk.common.Control;
import com.longrise.android.byjk.event.HraPaymentEvent;
import com.longrise.android.byjk.plugins.hra.CustomViewPager;
import com.longrise.android.byjk.plugins.hra.TimeUtil;
import com.longrise.android.byjk.plugins.hra.hratabthird.EndorseRefreshEvent;
import com.longrise.android.byjk.plugins.hra.hratabthird.HraCheckerBean;
import com.longrise.common.base.BaseActivity2;
import com.longrise.common.utils.AppUtil;
import com.longrise.common.utils.PrintLog;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/1/5.
 * Hra预约服务点详情
 */

public class HraServiceDetailActivity extends BaseActivity2<HraServiceDetailPresenter> implements HraServiceDetailContract.View, View.OnClickListener {

    private static final String TAG = "HraServiceDetailActivity";
    public static final String STOREID = "storeid";
    public static final String BESPEAKDATE = "bespeakdate";
    public static final String CHECKERBEAN = "checkerbean";
    private TabLayout mTb;
    private CustomViewPager mVp;
    private Button mBtnOrder;
    private HraPlacesFragment currentFragment;
    private HraPlacesPagerAdapter pagerAdapter;
    private String storeId;
    private TextView mStoreName;
    private TextView mStoreAdd;
    private TextView mStoreTel;
    private TextView mMachineType;
    private ImageView mStoreDis;
    private List<OrderPlaceParentBean> mParentBeanList = new ArrayList<>();
    private TextView mMachinePrice;
    private TextView mCheckType;
//    private String machineId;
    private HraServiceBean serviceBean;
    private String orderDate;
    private String timeslot;
    private String bespeakdate;
    private String timeslotid;
    private long nowMillis;
    private long nextMills;
    private List<OrderPlaceParentBean> mParentBean = new ArrayList<>();
    private int flags;
    private String areaid;
    private String orderdate;
    private String dateOfMonth;
    private HraCheckerBean mCheckerBean;
    private String price;
    private String machineType;
    private String machinModel;
    private String machinename;

    @Override
    public int getContentViewId(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_har_service_detail;
    }

    @Override
    public void initView() {
        serviceBean = new HraServiceBean();
        mTb = (TabLayout) findViewById(R.id.hra_point_tablayout);
        mVp = (CustomViewPager) findViewById(R.id.hra_point_viewpager);
        mBtnOrder = (Button) findViewById(R.id.btn_hra_order);
        mStoreName = (TextView) findViewById(R.id.tv_hra_service_name);
        mStoreAdd = (TextView) findViewById(R.id.tv_hra_service_area);
        mStoreTel = (TextView) findViewById(R.id.tv_hra_service_tel);
        mStoreDis = (ImageView) findViewById(R.id.iv_hra_service_dis);
        mCheckType = (TextView) findViewById(R.id.tv_hra_check_type);
        mMachinePrice = (TextView) findViewById(R.id.tv_hra_service_price);
        mMachineType = (TextView) findViewById(R.id.tv_hra_service_type);
        mBtnOrder.setEnabled(false);
        Control.setBookDataId("");
        setToolbarTitle(AppUtil.getString(R.string.hra_store_detail));
        getExtraData();
        EventBus.getDefault().register(this);
        initEvent();
        mPresenter.getStoreData(storeId);
        mPresenter.getMachineData(storeId);
        nowMillis = TimeUtil.getMillis(bespeakdate);
    }

    public void getExtraData(){
        Intent intent = getIntent();
        storeId = intent.getStringExtra(STOREID);
        bespeakdate = intent.getStringExtra(BESPEAKDATE);
        mCheckerBean = (HraCheckerBean) intent.getSerializableExtra(CHECKERBEAN);
        flags = intent.getFlags();
        PrintLog.e(TAG,"bespeakdate=:"+bespeakdate);
    }

    public void initDate(){
        String nextdate = "";
        for (int i = 0; i < 5; i++) {
            nextMills = (nowMillis + i * 24 * 60 * 60 * 1000);
            nextdate = TimeUtil.getFormatedDateTime("yyyy-MM-dd",nextMills);
            PrintLog.e(TAG,"nextdate=:"+nextdate);
            String customStr = TimeUtil.getCustomStr(nextdate);
            PrintLog.e(TAG,"customStr=:"+customStr);

            try {
                dateOfMonth = TimeUtil.getDateOfMonth(nextdate);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            String tabDate = customStr + dateOfMonth;
            PrintLog.e(TAG,"tabDate=:"+tabDate);
            OrderPlaceParentBean parentBean = new OrderPlaceParentBean();
            parentBean.setBespeakdate(nextdate);
            parentBean.setNewDate(tabDate);
            parentBean.setStoreid(storeId);
//            parentBean.setMachineid(machineId);
            PrintLog.e(TAG,"parentBean=:"+parentBean.toString());
            mParentBean.add(parentBean);
            initAdapter(mParentBean);
        }
        PrintLog.e(TAG,"mParentBeanList=:"+mParentBean.toString());
    }


    public void initAdapter(List<OrderPlaceParentBean> parentList) {
        mParentBeanList.clear();
        mParentBeanList.addAll(parentList);
        pagerAdapter = new HraPlacesPagerAdapter(getSupportFragmentManager());
        pagerAdapter.setData(parentList);
        mVp.setAdapter(pagerAdapter);
        mVp.setOffscreenPageLimit(4);
        mTb.setupWithViewPager(mVp);
    }


    public void initEvent(){
        mBtnOrder.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            //点击立即预约
            case R.id.btn_hra_order:
                start2OrderCheck();
                break;

        }
    }

    public void start2OrderCheck(){
        serviceBean.setTimeslot(timeslot);
        serviceBean.setOrderTime(orderDate);
        serviceBean.setTimeslotid(timeslotid);
//        serviceBean.setMachineId(machineId);
        serviceBean.setStoreid(storeId);
        serviceBean.setAreaId(areaid);
        serviceBean.setBespeakdate(orderdate);
        serviceBean.setMachineType(machineType);
        serviceBean.setPrice(price);
        serviceBean.setMachinemodel(machinModel);
        Intent intent = new Intent(this,HraOrderCheckActivity.class);
        Bundle mBundle = new Bundle();
        mBundle.putSerializable(HraOrderCheckActivity.HRAORDERCHECK,serviceBean);
        mBundle.putSerializable(HraOrderCheckActivity.CHECKERBEAN,mCheckerBean);
        intent.putExtras(mBundle);
        intent.setFlags(flags);
        startActivity(intent);
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void registerPosterEvent(HraServiceEvent event) {
        orderDate = event.getOrderDate();
        timeslot = event.getTimeslot();
        timeslotid = event.getTimeslotid();
        orderdate = event.getBespeakdate();
        price = event.getPrice();
        machineType = event.getMachineType();
        machinModel = event.getMachinModel();
        if (event.isSelected()) {
            mBtnOrder.setEnabled(true);
        }else{
            mBtnOrder.setEnabled(false);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void registerHraPaymentEvent(HraPaymentEvent event){
        if(event.isClose()){
            finish();
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void registEndorseEvent(EndorseRefreshEvent event){
        if(event.isClose()){ //关闭当前界面
            finish();
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN,sticky = true)
    public void registerHraPriceEvent(HraPriceEvent event){
        String price = event.getPrice();
        String machineType = event.getMachineType();
        mMachinePrice.setText("¥" + price);
        mMachineType.setText(machineType);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void onToolbarBackClick() {
        finish();
    }


    @Override
    public void refreshStoreData(EntityBean bean) {
        setData2View(bean);
    }

    //设置服务点信息
    public void setData2View(EntityBean bean) {
        EntityBean dataBean = bean.getBean("result");
        String storename = dataBean.getString("storename");
        String storeaddress = dataBean.getString("storeaddress");
        String storephone = dataBean.getString("storephone");
        String storepicurl = dataBean.getString("storepicurl");
        areaid = dataBean.getString("areaid");

        serviceBean.setStoreName(storename);
        serviceBean.setStoreAdd(storeaddress);
        serviceBean.setStorephone(storephone);

        mStoreName.setText(storename);
        mStoreAdd.setText(storeaddress);
        mStoreTel.setText(storephone);
        Glide.with(mContext)
                .load(storepicurl)
                .placeholder(mContext.getResources().getDrawable(R.drawable.by_banner))
                .crossFade()
                .into(mStoreDis);

    }

    @Override
    public void refreshMachineData(EntityBean bean) {
        EntityBean[] machineList = (EntityBean[]) bean.get("result");
        String price = machineList[0].getString("price");
//        machineId = machineList[0].getString("id");
        machinename = machineList[0].getString("machinemodelname");
        String machinemodel = machineList[0].getString("machinemodel");
        mCheckType.setText("HRA " +machinename);
        initDate();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void registCloseEvent(EndorseRefreshEvent event){
        if(event.isClose()){ //关闭界面
            finish();
        }
    }
}
